package bdhub.littletail.app.account.model;

import android.text.TextUtils;

import com.alibaba.fastjson.annotation.JSONField;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import bdhub.littletail.app.config.NetworkConfig;
import bdhub.littletail.lib.database.annotate.Transient;
import bdhub.littletail.lib.utils.DateUtils;
import bdhub.littletail.lib.utils.StringUtil;
import se.emilsjolander.sprinkles.Model;
import se.emilsjolander.sprinkles.annotations.Key;
import se.emilsjolander.sprinkles.annotations.Table;

@Table("Users")
public class User extends Model {

	@Key
	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_UID)
	private long id;
	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_NICKNAME)
	private String nickName;
	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_PHONE_NUMBER)
	private String phoneNumber;
	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_AVATAR)
	private String avatar;
	@Transient
	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_OPENACC)
	private String openAcc;
	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_GENDER)
	private String gender;
	@JSONField(format = "yyyyMMdd", name = NetworkConfig.PARAM_ACCOUNT_BIRTHDAY)
	private Date birthday;
	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_PROVINCE)
	private String province;
	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_CITY)
	private String city;
	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_SIGN)
	private String sign;
	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_WECHAT)
	private String wechat;
	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_QQ)
	private String qq;
	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_WEIBO)
	private String weibo;
	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_ALBUM)
	private String album;
	// private int loginType;
	// private HashMap<String, SNSUser> snsInfo = new HashMap<String,
	// SNSUser>();

	private boolean isFriend = false;
	private int friendType = -1;
	// private boolean requestFriend;

	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_CURRENT_ROOM_NAME)
	private String currentRoomName;
	@Transient
	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_CURRENT_ROOM_ID)
	private long currentRoomId;

	/** 用户名字的拼音，用来做索引 */
	private String pinyin;

	/** 第三方授权信息（暂未用到） */
	@JSONField(serialize = false)
	private String authData;

	/** 个人背景 */
	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_BACKGROUND_IMAGE)
	private String backgroundImage;
	/** 备用 */
	@JSONField(serialize = false)
	private String reserve;

	@Transient
	/** 是否被禁言 */
	private boolean isBlack;
	
	@Transient
	/** 是否被拉黑 */
	private boolean black;

	@Transient
	/** 服务端返回的推荐火聊入口文案 */
	private String shareRecommendWord;
	@Transient
	/** 针对该用户是否有新的推荐文案 */
	private boolean hasNewShareRecommendWord;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getNickName() {
		if (null == nickName) {
			return "";
		}
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

//	public String getReadableGender() {
//		if (StringUtil.isEmpty(gender)) {
//			return LittleTailApplication.sharedInstance().getString(R.string.female);
//		} else {
//			return gender.equals("M") ? LittleTailApplication.sharedInstance().getString(
//					R.string.male) : LittleTailApplication.sharedInstance().getString(
//					R.string.female);
//		}
//	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	// FastJson 转换时间，这里貌似有 BUG ，没有使用声明的 format ，直接设置了毫秒值，故增加以下方法避免这个问题。
	@Transient
	private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
			"yyyyMMdd");

	public void setBirthday(String birthday) {
		try {
			this.birthday = simpleDateFormat.parse(birthday);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public String getProvince() {
		return parseString(province);
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return parseString(city);
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getWechat() {
		return wechat;
	}

	public void setWechat(String wechat) {
		this.wechat = wechat;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public String getWeibo() {
		return weibo;
	}

	public void setWeibo(String weibo) {
		this.weibo = weibo;
	}

	public String getAlbum() {
		return album;
	}

	public void setAlbum(String album) {
		this.album = album;
	}

	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_IS_FRIEND)
	public boolean isFriend() {
		return isFriend;
	}

	@JSONField(name = NetworkConfig.PARAM_ACCOUNT_IS_FRIEND)
	public void setFriend(String isFriend) {
		this.isFriend = isFriend.equals(NetworkConfig.VALUE_COMMON_TRUE);
	}

	public int getFriendType() {
		return friendType;
	}

	public void setFriendType(int friendType) {
		this.friendType = friendType;
	}

	public void setPinyin(String pinyin) {
		this.pinyin = pinyin;
	}

	@JSONField(serialize = false)
	public String getPinyin() {
		if (null == pinyin) {
			String text = nickName;
			if (!StringUtil.isEmpty(text)) {
				text = text.replaceFirst("@.*$", "");
				pinyin = StringUtil.generatePinyin(text).get(0);
			}
		}
		if (null == pinyin) {
			pinyin = "";
		}
		return pinyin;
	}

	public String getBackgroundImage() {
		return backgroundImage;
	}

	public void setBackgroundImage(String backgroundImage) {
		this.backgroundImage = backgroundImage;
	}

	public String getCurrentRoomName() {
		return currentRoomName;
	}

	public void setCurrentRoomName(String currentRoomName) {
		this.currentRoomName = currentRoomName;
	}

	public long getCurrentRoomId() {
		return currentRoomId;
	}

	public void setCurrentRoomId(long currentRoomId) {
		this.currentRoomId = currentRoomId;
	}

	@JSONField(serialize = false)
	public String getAge() {
		if (null == birthday) {
			return "";
		}
		return String.valueOf(DateUtils.getAge(birthday));
	}

	public boolean getIsBlack() {
		return isBlack;
	}

	public void setIsBlack(String isBlack) {
		this.isBlack = NetworkConfig.getLocalBoolean(isBlack);
	}
	
	public boolean getBlack() {
		return black;
	}
	
	public void setBlack(String black) {
		this.black = NetworkConfig.getLocalBoolean(black);
	}

	public String getShareRecommendWord() {
		return shareRecommendWord;
	}

	public void setShareRecommendWord(String shareRecommendWord) {
		this.shareRecommendWord = shareRecommendWord;
	}

	public boolean isHasNewShareRecommendWord() {
		return hasNewShareRecommendWord;
	}

	public void setHasNewShareRecommendWord(boolean hasNewShareRecommendWord) {
		this.hasNewShareRecommendWord = hasNewShareRecommendWord;
	}

	private String parseString(String str) {
		if (TextUtils.isEmpty(str) || "null".equals(str)) {
			return "";
		}
		return str;
	}
	
	/**
	 * 同步数据
	 *
	 * @param user
	 */
	public void sync(User user) {
		setFriend(NetworkConfig.getServerBoolean(user.isFriend()));
		setFriendType(user.getFriendType());
		setNickName(user.getNickName());
		setGender(user.getGender());
		setBirthday(user.getBirthday());
		setProvince(user.getProvince());
		setCity(user.getCity());
		setSign(user.getSign());
		setAvatar(user.getAvatar());
		setBackgroundImage(user.getBackgroundImage());
		setPhoneNumber(user.getPhoneNumber());
	}

	@Override
	public boolean equals(Object o) {
		return o instanceof User && ((User) o).getId() == getId();
	}
}
